Skip to content

Fix up core count detection logic on android - #131234

Open
eduardo-vp wants to merge 5 commits into
dotnet:mainfrom
eduardo-vp:fix-android-core
Open

Fix up core count detection logic on android#131234
eduardo-vp wants to merge 5 commits into
dotnet:mainfrom
eduardo-vp:fix-android-core

Conversation

@eduardo-vp

@eduardo-vp eduardo-vp commented Jul 22, 2026

Copy link
Copy Markdown
Member

Parse /sys/devices/system/cpu/present as a comma-separated cpulist (single indices and ranges) rather than assuming a single 0-(n-1) range.

Closes #126417.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Mono’s Android CPU core-count detection to correctly parse the kernel’s /sys/devices/system/cpu/present “cpulist” format (including non-contiguous ranges), improving the accuracy of mono_cpu_count() on Android.

Changes:

  • Parse /sys/devices/system/cpu/present as a comma-separated cpulist (single indices and ranges) rather than assuming a single 0-(n-1) range.
  • Increase the read buffer size and add parsing success gating so malformed/partial reads fall back to existing platform logic.
  • Clarify the comment describing the /sys entry format with concrete examples.

@jkotas

jkotas commented Jul 22, 2026

Copy link
Copy Markdown
Member

Do we need the same logic for CoreCLR/NAOT on Android?

@akoeplinger

Copy link
Copy Markdown
Member

judging from my long comment a few lines below the changed lines it sounds like we should be fine with _SC_NPROCESSORS_CONF which is already used by coreclr from what I can see.

though I see some inconsistency there, gcenv.unix.cpp and sysinfo.cpp use _SC_NPROCESSORS_CONF on arm/arm64/loongarch64/riscv64 and _SC_NPROCESSORS_ONLN on other architectures

@eduardo-vp

Copy link
Copy Markdown
Member Author

Currently mono on android tries to read /sys/devices/system/cpu/present and fallbacks to _SC_NPROCESSORS_CONF which should be the same value. I think having this behavior in CoreCLR/Native AOT for consistency would be fine. In general looks like that's better than relying on sched_getaffinity/_SC_NPROCESSORS_ONLN which can return fewer cores than the program can actually leverage because of android's aggressive hot unplugging.

Copilot AI review requested due to automatic review settings July 24, 2026 19:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread src/coreclr/pal/src/misc/sysinfo.cpp Outdated
Comment thread src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp
Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Outdated
Comment thread src/native/minipal/cpucount.h
Copilot AI review requested due to automatic review settings July 25, 2026 00:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment thread src/native/minipal/cpucount.h Outdated
Comment thread src/coreclr/pal/src/misc/sysinfo.cpp Outdated
Comment thread src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp
Copilot AI review requested due to automatic review settings July 25, 2026 00:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread src/native/minipal/cpucount.c Outdated
Comment thread src/coreclr/pal/src/misc/sysinfo.cpp Outdated
Comment thread src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp Outdated
Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 25, 2026 01:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/native/minipal/cpucount.c:49

  • parse_cpulist_file should validate that parsed CPU indices are non-negative and should avoid signed-overflow UB when accumulating count. As written, negative indices (or very large ranges) could produce maxIndex == -1 or overflow count, which then flows into CPU set sizing and other CPU-count logic.
        if (hi < lo)
        {
            // Invalid range, discard
            count = 0;
            break;
        }

        count += hi - lo + 1;
        if (hi > maxIndex)
        {
            maxIndex = hi;
        }

Comment thread src/native/minipal/cpucount.c
@eduardo-vp

Copy link
Copy Markdown
Member Author

/azp run runtime-androidemulator, runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[android] Test failure: check core count detection logic

4 participants